package com.youth.hrsync.config.batch;

import com.kcis.xian.hrsync.mapper.kunshan.KunShanCheckinoutMapper;
import com.kcis.xian.hrsync.mapper.kunshan.KunShanUserinfoMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.StepContribution;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.job.builder.FlowBuilder;
import org.springframework.batch.core.job.flow.Flow;
import org.springframework.batch.core.scope.context.ChunkContext;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableBatchProcessing
public class HrDelBatchConfig extends DefaultBatchConfig {

    private Logger logger = LoggerFactory.getLogger(HrDelBatchConfig.class);

    @Autowired
    KunShanUserinfoMapper kunShanUserinfoMapper;

    @Autowired
    KunShanCheckinoutMapper kunShanCheckinoutMapper;

    @Bean
    public Tasklet deleteUserTasklet() {
        return new Tasklet() {
            @Override
            public RepeatStatus execute(StepContribution stepContribution, ChunkContext chunkContext) throws Exception {
                try {
                    kunShanUserinfoMapper.deleteUsers();
                    return RepeatStatus.FINISHED;
                }catch (Exception e) {
                    chunkContext.getStepContext().getStepExecution().setTerminateOnly(); //stepExecution设置停止标记，Spring Batch 识别后直接停止步骤，进而停止流程
                    logger.error(e.getMessage());
                    throw e;
                }
            }
        };
    }

    @Bean
    public Step delUserStep() {
        return stepBuilderFactory.get("delUserStep")
                .tasklet(deleteUserTasklet())
                .build();
    }

    @Bean
    public Tasklet delCheckinoutTasklet() {
        return new Tasklet() {
            @Override
            public RepeatStatus execute(StepContribution stepContribution, ChunkContext chunkContext) throws Exception {
                try {
                    kunShanCheckinoutMapper.deleteCheckinout();
                    return RepeatStatus.FINISHED;
                }catch (Exception e) {
                    logger.error(e.getMessage());
                    throw e;
                }

            }
        };
    }

    @Bean
    public Step delCheckinoutStep() {
        return stepBuilderFactory.get("delCheckinoutStep")
                .tasklet(delCheckinoutTasklet())
                .build();
    }

    @Bean
    public Flow delFlow() {
        return new FlowBuilder<Flow>("delFlow")
                .start(delUserStep())
                .next(delCheckinoutStep())
                .build();
    }


    @Bean
    public Step delStep() {
        return stepBuilderFactory.get("delStep")
                .flow(delFlow())
                .build();
    }

}
